package com.yjhb.android.sbz.mvp.bidding;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.entity.BiddingUser;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/8 16:21
 */
public class BiddingListPresenter extends ConventionPresenter<BiddingView> {
    private final String TAG = "BiddingListPresenter";

    public BiddingListPresenter(BiddingView biddingView) {
        super(biddingView);
    }

    @Override
    protected void onDestroy() {

    }


    public void getMyBidding() {
        HttpTool.request(model.getMyBidding(), new HttpObserver<BiddingUser>() {

            @Override
            public void onResult(BiddingUser data) {
                if (view != null) {
                    view.onMyBidding(data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onMyBidding(null);
                }
            }
        });
    }

    public void getYesterdayBidding() {
        HttpTool.request(model.getYesterdayBidding(), new HttpObserver<BiddingUser>() {
            @Override
            public void onResult(BiddingUser data) {
                final BiddingUser user = data;
                if (user != null) {
                    HttpTool.request(model.getHomeTops(App.userInfo.getId()), new HttpObserver<List<TaskInfo>>() {

                        @Override
                        public void onResult(List<TaskInfo> data) {
                            if (view != null) {
                                if (data.size() > 0) {
                                    user.setName(data.get(0).getName());
                                    view.onMyOldBidding(user);
                                } else {
                                    view.onMyOldBidding(null);
                                }
                            }
                        }

                        @Override
                        public void onError(int code, String msg) {
                            if (view != null) {
                                view.onMyOldBidding(null);
                            }
                        }
                    });
                } else {
                    if (view != null) {
                        view.onMyBidding(null);
                    }
                }
            }
            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onMyOldBidding(null);
                }
            }
        });


    }


    public void getBidList() {
        HttpTool.request(model.getBidList(4, 1), new HttpObserver<List<BiddingUser>>() {
            @Override
            public void onResult(List<BiddingUser> data) {
                if (view != null) {
                    view.onSuccess(data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    public void adsChange(Integer taskId) {
        HttpTool.request(model.adsChange(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onChangeTask();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


}
